package bg.ittalents.homework.airport;

public final class StringHelper {

	public static int getFirstInt(String text) {
		String number = "";

		boolean capturedNumber = false;

		for (int i = 0; i < text.length(); i++) {

			if (capturedNumber && !isDigit(text.charAt(i))) {
				break;
			}

			if (isDigit(text.charAt(i))) {
				capturedNumber = true;
				number += text.charAt(i);
			}
		}

		if (!number.equals("")) {
			return Integer.parseInt(number);
		}

		return -1;
	}

	public static int getLastInt(String text) {
		String number = "";

		boolean capturedNumber = false;

		for (int i = text.length()-1; i >=0; i--) {

			if (capturedNumber && !isDigit(text.charAt(i))) {
				break;
			}

			if (isDigit(text.charAt(i))) {
				capturedNumber = true;
				number += text.charAt(i);
			}
		}
		
		number=new StringBuilder(number).reverse().toString();

		if (!number.equals("")) {
			return Integer.parseInt(number);
		}

		return -1;
	}

	private static boolean isDigit(char c) {
		return c <= '9' && c >= '0';
	}
}
